package com.handpay.nocardpay.baselibrary.qualificationinfo.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.handpay.nocardpay.baselibrary.R;
import com.handpay.nocardpay.baselibrary.qualificationinfo.model.bean.Bank;
import com.handpay.nocardpay.baselibrary.qualificationinfo.model.bean.SelectedBank;

import java.util.ArrayList;
import java.util.List;

public class BankAdapter extends BaseAdapter {
	
	List<Bank> mBanks = new ArrayList<Bank>(100);
	LayoutInflater mLI = null;
	Context hpcontext;
	public BankAdapter(Context c) {
		mLI = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		hpcontext = c;
	}
	public synchronized void refreshBanks(List<Bank> banks) {
		mBanks.clear();
		Bank pleaseSelectBank = new Bank();
		pleaseSelectBank.setBankName(hpcontext.getString(R.string.public_please_select_Bank));
		pleaseSelectBank.setBankCode("-1");
		pleaseSelectBank.setAreaFlag("0");
		pleaseSelectBank.setProvinceFlag("0");
		pleaseSelectBank.setSubBankFlag("0");
		banks.add(pleaseSelectBank);
		mBanks.addAll(banks);
		this.notifyDataSetChanged();
	}

	/**
	 * 选中
	 * @param bankCode
	 * @return 返回选中的index和数据【index用于spinner自动选择 ，数据用于交互 】
	 */
	public synchronized SelectedBank selectedByBankCode(String bankCode){
		SelectedBank selectedBank=new SelectedBank();
		for(int i=0;i<mBanks.size();i++){
			if(mBanks.get(i)!=null&&bankCode.equals(mBanks.get(i).getBankCode())){
				selectedBank.setIndex(i);
				selectedBank.setBank(mBanks.get(i));
				break;
			}
		}
		return selectedBank;
	}
	public synchronized void clearBank() {
		mBanks.clear();
	}
	@Override
	public int getCount() {
		return mBanks.size() > 0 ? mBanks.size()-1 : 0;
	}

	@Override
	public Bank getItem(int position) {
		return mBanks.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	// 用于其他
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View container = null;
		if(null == convertView) {
			convertView = mLI.inflate(R.layout.bank_item, null);
		}
		container = convertView;
		container.findViewById(R.id.ivBankLogo).setVisibility(View.GONE);
		((TextView)container.findViewById(R.id.tvBankName)).setText(mBanks.get(position).getBankName());
		container.findViewById(R.id.ivChecked).setVisibility(View.GONE);
		return container;
	}
    // 用于spinner
	@Override
	public View getDropDownView(int position, View convertView, ViewGroup parent) {
		View container = null;
		if(null == convertView) {
			convertView = mLI.inflate(R.layout.bank_item, null);
		}
		container = convertView;
		container.findViewById(R.id.ivBankLogo).setVisibility(View.GONE);
		TextView tvbankname = (TextView)container.findViewById(R.id.tvBankName);
		tvbankname.setText(mBanks.get(position).getBankName());
		tvbankname.setTextColor(hpcontext.getResources().getColorStateList(R.color.black_color));
		container.findViewById(R.id.ivChecked).setVisibility(View.GONE);
		return container;
	}

}
